#include "ProductTestFeed.h"

Com_pvoid ProductTest_Waiting_For_Result_Thread(Com_pvoid arg)
{
    Com_s32 ret = -1;
    SMsgAVIoctrlUserEvent result;
    Com_s32 index = (int)arg;

    memset(&result,0,sizeof(result));
    Com_u8 cnt = 0;

    while(Ext_ProductTest_Uart_Get_Feed_Flag() != DEF_COM_TRUE)
    {
        if(cnt >= 150)
        {
            printf("timeout of get feed result\n");
            break;
        }
        cnt++;
        usleep(100 * 1000);
    }

    ret = Ext_ProductTest_Uart_Get_FeedResult();
    if(ret == DEF_COM_TRUE)
    {
        result.event = 0x42;
    }
    else
    {
        result.event = 0x34;
    }

    ret = avSendIOCtrl(index, IOTYPE_USER_IPCAM_DEVICE_USER_EVENT_REPORT,
                       (char *)&result, sizeof(SMsgAVIoctrlUserEvent));
    if(ret < 0)
    {
        printf("ret : %x\n",ret);
    }

    return NULL;
}

int Handle_IOTYPE_USER_FEED_COMMAND(int sid, int av_index, char* buf)
{
    pthread_t id;

    Ext_ProductTest_Uart_Set_FeedResult(DEF_COM_FALSE);
    Ext_ProductTest_Uart_Set_Feed_Flag(DEF_COM_FALSE);

    pthread_create(&id, NULL, ProductTest_Waiting_For_Result_Thread,(void *)av_index);
    pthread_detach(id);

    return 0;
}
